﻿package com.tomvian.twodee.sideon.actions 
{
	import com.tomvian.twodee.common.actions.Action;
	import com.tomvian.twodee.common.Actor;
	import com.tomvian.twodee.common.interactions.Interaction;
	
	/**
	 * DoubleJumping
	 * @author Thomas Vian
	 */
	public class DoubleJumping extends Action
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public static const DISPLAY_DOUBLE_JUMPING:String = "doubleJumping";
		
		public var ground:Interaction;
		
		public var airAccel:Number;
		public var jumpSpeed:Number;
		public var minSpeed:Number;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function DoubleJumping(	actor:Actor, 
										ground:Interaction, 
										airAccel:Number,
										jumpSpeed:Number, 
										minSpeed:Number = Number.NEGATIVE_INFINITY) 
		{
			super(actor);
			
			this.ground = ground;
			
			this.airAccel = airAccel;
			this.jumpSpeed = jumpSpeed;
			this.minSpeed = minSpeed;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function check(state:uint):void 
		{
			if (actorController.keyPress.jump && actorMass.velocity.y > minSpeed)
			{
				actor.currentAction = this;
			}
		}
		
		override public function start():void 
		{
			actorMass.velocity.y = jumpSpeed;
		}
		
		override public function act():void 
		{
			if (enabled)
			{
				if (!ground.interacting)
				{
					if (actorController.right) 	actorMass.velocity.x += airAccel;
					if (actorController.left) 	actorMass.velocity.x -= airAccel;
				}
				else
				{
					actor.returnToDefault();
				}
			}
		}
		
		override public function display():void 
		{
			actorState.state = DISPLAY_DOUBLE_JUMPING;
		}
	}
}